package kt.com.bilin.coin.ad

import android.view.ViewGroup
import androidx.activity.ComponentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.bilin.coin.BuildConfig
import kt.com.bilin.coin.common.toastMessageLong
import kt.com.bilin.coin.ui.dialog.LoadingDialog
import kt.com.bilin.coin.utils.Logger


typealias OnVideoComplete = () -> Unit

object FusionAdManager : LifecycleObserver {
    private var mLoadingDialog: LoadingDialog? = null
    private var mOnVideoComplete: OnVideoComplete? = null
    private var mActivity: ComponentActivity? = null
//    private var mRewardVideoAd: RewardVideoAd? = null
    fun loadRewardVideoAd(activity: ComponentActivity?, onVideoComplete: OnVideoComplete) {
        if (!changeActivity(activity)) return
        mOnVideoComplete = onVideoComplete
        mActivity?.lifecycle?.addObserver(this)
        mLoadingDialog?.show()
//        val adCode = AdCode.Builder()
//                .setCodeId("192323661384908800")
//                .setOrientation(AdCode.Orientation.VERTICAL)
//                .build()
//        FusionAdSDK.loadRewardVideoAd(mActivity, adCode, object : RewardVideoAdListener() {
//            override fun onError(type: Int, code: Int, msg: String?) {
//                if (BuildConfig.DEBUG)
//                    msg.toastMessageLong()
//                Logger.e("msg==>${msg}")
//                onDestroy()
//            }
//
//            override fun onRewardVideoAdLoad(rewardVideoAd: RewardVideoAd?) {
//                mRewardVideoAd = rewardVideoAd
//            }
//
//            override fun onRewardVideoCached() {
//                mActivity?.let { mRewardVideoAd?.show(it) }
//            }
//
//            override fun onReward() {
//                super.onReward()
//                mOnVideoComplete?.invoke()
//                onDestroy()
//            }
//
//            override fun onVideoError() {
//                super.onVideoError()
//                onDestroy()
//            }
//
//            override fun onAdClosed() {
//                super.onAdClosed()
//                onDestroy()
//            }
//        })
    }

    fun loadSplashAd(activity: ComponentActivity?, viewGroup: ViewGroup, onAdComplete: OnVideoComplete) {
        if (!changeActivity(activity)) return
        mOnVideoComplete = onAdComplete
//        val adCode = AdCode.Builder()
//                .setCodeId("192323548696543232")
//                .setImgAcceptedSize(1080, 1920)
//                .build()
//
//        FusionAdSDK.loadSplashAd(mActivity, adCode, viewGroup, object : SplashAdListener {
//            override fun onError(type: Int, code: Int, msg: String?) {
//                Logger.e("msg==>${msg}")
//                onAdComplete()
//            }
//            override fun onSplashAdLoad(splashAd: SplashAd?) {}
//            override fun onAdClicked() {}
//            override fun onAdShow() {}
//            override fun onAdClosed() = onAdComplete()
//            override fun onADTick(p0: Long) {}
//        })
    }

    private fun onAdComplete() {
        mOnVideoComplete?.invoke()
        onDestroy()
    }

    private fun changeActivity(activity: ComponentActivity?): Boolean {
        if (activity == null) return false
        mActivity?.let { onDestroy() }
        mLoadingDialog?.dismiss()
        mLoadingDialog = null
        mActivity = activity
        mLoadingDialog = LoadingDialog(activity).apply { setCancelable(false) }
        return true
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy() {
        mActivity?.lifecycle?.removeObserver(this)
        mActivity = null
        mOnVideoComplete = null
        mLoadingDialog?.dismiss()
        mLoadingDialog = null
//        mRewardVideoAd = null
    }
}

/*
open class RewardVideoAdListener : com.touchxd.fusionsdk.ads.rewardvideo.RewardVideoAdListener {
    override fun onError(p0: Int, p1: Int, p2: String?) {

    }

    override fun onRewardVideoAdLoad(rewardVideoAd: RewardVideoAd?) {

    }

    override fun onRewardVideoCached() {
        Logger.e("onRewardVideoCached")
    }

    override fun onAdShow() {
        Logger.e("onAdShow")
    }

    override fun onAdClicked() {
        Logger.e("onAdClicked")
    }

    override fun onAdClosed() {
        Logger.e("onAdClosed")
    }

    override fun onVideoComplete() {

    }

    override fun onVideoError() {
        Logger.e("onVideoError")
    }

    override fun onReward() {
        Logger.e("onReward")
    }

}*/
